﻿// -----------------------------------------------------------------------
// <copyright file="UserDtoMapperExtension.cs">
// Copyright (c) 2014 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

using System.Linq;

namespace Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.Mappers
{
	using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.Database.Dto;
	using Moveax.DataAccess.Cqrs.Highway.UnitTests.Model;

	internal static class UserDtoMapperExtension
	{
		public static User ToModel(this UserDto source)
		{
			return source.ToModel(ignoreNavigation: false);
		}

		public static User ToModel(this UserDto source, bool ignoreNavigation)
		{
			if (source == null)
				return null;

			var user = new User() {
				// In alphabetical order without navigation properties
				Created = source.Created,
				Email = source.Email,
				Id = source.Id,
				IsDisabled = source.IsDisabled,
				Name = source.Name,
				Type = source.Type
			};

			if (source.Links != null && !ignoreNavigation)
				user.Links = source.Links.Select(l => l.ToModel(ignoreNavigation: true)).ToArray();

			return user;
		}
	}
}
